<script type='text/javascript' src='https://www.google.com/jsapi'></script>
		<script type='text/javascript'>
          google.load('visualization', '1', {packages:['table']});
         
		  
		  var data = new google.visualization.DataTable();
		  
          function drawTable(data) {
            data.addColumn('string', 'Nume furnizor');
            data.addColumn('string', 'CUI/CF');
            data.addColumn('string', 'Nr decizie autorizare');
			data.addColumn('string', 'Localitate');
			data.addColumn('string', 'Telefon');
			data.addColumn('string', 'Email');
			<?php
			require_once ('./mysql_connect.php'); 
			$rez = mysqli_query($dbc,"SELECT `nume`,`CUI_CF`,`numar_decizie_autorizare`,`localitate`,`telefon`,`email` FROM anc.furnizor");	
			while ($rand = mysqli_fetch_assoc($rez))
				{ 
			?>	
			 data.addRows([
              ['<?php echo $rand['nume']; ?>', 
               '<?php echo $rand['CUI_CF']; ?>',  
               '<?php echo $rand['numar_decizie_autorizare']; ?>',
               '<?php echo $rand['localitate']; ?>',
			   '<?php echo $rand['telefon']; ?>',
			   '<?php echo $rand['email']; ?>' ]
            ]); 
			<?php
				}
			?>
	

          }
		  
		  var table = new google.visualization.Table(document.getElementById('table_div'));
          table.draw(data, {showRowNumber: true});
		  google.setOnLoadCallback(drawTable(data)); //TO DO
		 
 </script>
 
 <!--//TO DO -->
<form method="post" action="formular_add_furnizor.php" > 
    <table title="Cauta furnizor" border="0" width="1000px" height="100px">
    <tr>
        <td>
            <table title="Cauta" border="0" width="300px" height="100px">
            <tr>
                <td>CUI/CF</td>	
                <td>
                    <input name="searched_cui_cf" />
                </td>
            </tr>
            <tr>
                <td>Nume furnizor</td>        	
                <td>
                    <input name="provider_name" />
                </td>
            </tr>
            <tr>
                <td>	
                </td>
                <td>
                    <input type="submit" value="Cauta" />
                </td>
            </tr>
            
            </table>
        
        </td>
        <td>
            <div class="table_div" id="table_div" >
            </div>
        </td>
    </tr>
    </table>
</form>

<table>
	<tr>
	</tr>
    <tr>
	</tr>
	<tr>
	</tr>
    <tr>
	</tr>
</table>

<?php              
define('DEFAULT_TABLE_ROWS', 1);

include "includes".DIRECTORY_SEPARATOR."add_furnizor_functions.php";

include "includes".DIRECTORY_SEPARATOR."furnizor_forms_for_tables.php";
			
				
$form_fields_with_speficications = array();

for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
{
	foreach( $form_for_table as $form_column_table )
	{
		array_push( $form_fields_with_speficications,$form_column_table);
	}
}



		
	
# function to search the provider when the "Cauta" button is pressed
function search_provider(){

}
	
foreach($form_fields_with_speficications as $form_key => $form_value)
{
	if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
			$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
			$form_fields_with_speficications[$form_key]['field_size'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
			$form_fields_with_speficications[$form_key]['max_length'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
			$form_fields_with_speficications[$form_key]['colors'] = null;						  
	if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
			$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
	
	if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
	{
		if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
			$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
		else
			$form_fields_with_speficications[$form_key]['current_value'] =  '';
	}
	else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
	{
		$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
	}
	
	$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
}

$form_content = generate_form_content($form_fields_with_speficications);

$erori = "";

$get_form=<<<GATA
	<form method="post" action = "formular_add_furnizor.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
	
function has_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return true;
	else 
		return false;
}

function get_value($input_name){
	if ( isset($_POST[$input_name]) && !empty($_POST[$input_name]))
		return $_POST[$input_name];
	else 
		return NULL;
}

# search into table "furnizor" by CUI/CF
function search_by_cui($search_value, $dbc){
	echo $search_value;
	require_once ('mysql_connect.php');
	$rezs = mysqli_query($dbc, "SELECT `nume`,`CUI_CF`,`numar_decizie_autorizare`,`localitate`,`telefon`,`email` FROM anc.furnizor WHERE `CUI_CF` like '%$search_value%'");
	while ($rows = mysqli_fetch_assoc($rezs)){ 
		//echo "in search by cui";
		//echo $rows['nume'];
		?>
        <script type='text/javascript'>	
		 data.addRows([
		  ['<?php echo $rows['nume']; ?>', 
		   '<?php echo $rows['CUI_CF']; ?>',  
		   '<?php echo $rows['numar_decizie_autorizare']; ?>',
		   '<?php echo $rows['localitate']; ?>',
		   '<?php echo $rows['telefon']; ?>',
		   '<?php echo $rows['email']; ?>' ]
		]); 
		</script>
		<?php
	}
}

function search_by_name($search_value){
	
}

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $get_form;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{

	if ( has_value("searched_cui_cf") || has_value("provider_name")){
	
		echo "SEARCH";
		$sccv = get_value("searched_cui_cf") ;
		
		if ( has_value("searched_cui_cf") && $sccv!=NULL ){
			
			search_by_cui($sccv,$dbc);
		}
			
		echo $get_form;
	}
	else{	
		$ok = 1;
			foreach($form_fields_with_speficications as $form_key => $form_value)
			{
			if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
			{									
				if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
				|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
				//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
				{
					# Something went wrong with this form element so an error will be displayed to user
					$ok = 0;
					
					# Change color for element ; default will be red
					$form_fields_with_speficications[$form_key]['current_color'] = "red";
					if( isset($form_fields_with_speficications[$form_key]['colors'] ))
					{
						if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
						{
							$form_fields_with_speficications[$form_key]['current_color'] = 
							$form_fields_with_speficications[$form_key]['colors']['second'];		
						}
					}
					
					# Generate an error if nothing it's provided
					$default_generated_error = "";
					
					if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
					{
						$default_generated_error = 
						"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
					{
						$default_generated_error =
						 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else
					{
						$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
					}
					
					# Add the provided error or the default one
					if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
					{
						if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
						{
							$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
					
					continue;
				}
				
				# Check regex here
				if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
				{
					if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
					{
						if( preg_match($form_fields_with_speficications[$form_key]['regex'],
						$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."sunt invalide</br>";
							}
							else
							{
								$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				
				# check validation functions
				if ( isset( $form_fields_with_speficications[$form_key]['validation_function'] ) )
				{
					if ( !empty( $form_fields_with_speficications[$form_key]['validation_function'] ) )
					{
						$function_name = $form_fields_with_speficications[$form_key]['validation_function'] ;
						$cif_value = $_POST[$form_fields_with_speficications[$form_key]['post_name']]; 
						
						if($function_name($cif_value) == 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = 
								"Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."
								 sunt invalide</br>";
							}
							else
							{
								$default_generated_error = 
								"Campul ".$form_fields_with_speficications[$form_key]['post_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = 
										$erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
			}
		}
		
		if($ok != 1)
		{
			$form_content = generate_form_content($form_fields_with_speficications);
			$form_post=<<<GATA
				<form method="post" action = "formular_add_furnizor.php">
					<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
					$form_content
					<input type="hidden" name="number_of_rows" value="3"> 
					<tr>
						<td><input type ="submit" name="previzualizare" value = "Previzualizare" 			 			
						onclick="this.form.target='_blank';return true;">
						</td>
						<td><input type ="submit" value = "Salveaza" 
						onclick="this.form.target='_self';return true;" name="comanda">
						</td>
					</tr>
			</table>
			</form>
GATA;
			echo $form_post;
			echo "<font color=\"red\">".$erori."</font>";
		}
		else  
		{
			require_once ('mysql_connect.php');
			$u = mysqli_query($dbc,"INSERT INTO `furnizor`
			(`nume`,`CUI_CF`,`numar_decizie_autorizare`,`data_decizie_autorizare`,`forma_organizare`, 				 	   									 		`forma_proprietate`,`smac`,`regiune`,`judet`,`localizare`,`localitate`,`strada`,`nr_strada`,`bloc`,
			`scara`,`etaj`,`apartament`,`telefon`,`fax`,`email`,`website`)  VALUES ('$_POST[nume_furnizor]', '$_POST[CUI_CF]',  											 		'$_POST[numar_decizie_autorizare]','$_POST[data_decizie_autorizare]',   						  				   	 			 		'$_POST[forma_organizare]','$_POST[forma_proprietate]','$_POST[smac]', '$_POST[regiune]',    																							 		'$_POST[judet]','$_POST[localizare]', '$_POST[localitate]','$_POST[strada]', '$_POST[nr_strada]', '$_POST[bloc]',  			 		'$_POST[scara]', '$_POST[etaj]','$_POST[apartament]', '$_POST[telefon]', '$_POST[fax]', '$_POST[email]', '$_POST[site]')");
			
			echo "<br />";	
			echo "<center><h2><font color=\"#00CC00\">Furnizorul a fost adaugat cu succes ! </font></h2></center>";	
			echo "<br />";	
			echo $get_form;
		}
	}
}
				
?> 